﻿using System;
using System.Globalization;
using System.Xml.Linq;

namespace ATCAssistant
{
    public class Restriction
    {
        public Restriction(XElement rest)
        {
            this.RunwayId = rest.Element("runwayId").Value;
            this.Reason = this.ParseType(rest);
            this.Status = this.ParseStatus(rest);

            if (!String.IsNullOrEmpty(rest.Element("start").Value))
                this.StartTime = TimeSpan.ParseExact(rest.Element("start").Value, "hhmm", CultureInfo.CurrentCulture);

            if (!String.IsNullOrEmpty(rest.Element("stop").Value))
                this.StopTime = TimeSpan.ParseExact(rest.Element("stop").Value, "hhmm", CultureInfo.CurrentCulture);
        }

        public string RunwayId { get; private set; }
        public RestrictionType Reason { get; private set; }
        public RestrictionStatus Status { get; private set; }
        public TimeSpan StartTime { get; private set; }
        public TimeSpan StopTime { get; private set; }

        private RestrictionStatus ParseStatus(XElement rest)
        {
            switch (rest.Element("status").Value.ToLower())
            {
                case "available":
                    return RestrictionStatus.Available;
                case "pilot request":
                    return RestrictionStatus.PilotRequest;
                case "closed":
                    return RestrictionStatus.Closed;
                default:
                    return RestrictionStatus.Available;
            }
        }

        private RestrictionType ParseType(XElement rest)
        {
            string value = rest.Element("type").Value;
            switch (value.ToLower())
            {
                case "noise abatement":
                    return RestrictionType.NoiseAbatement;
                case "general sop":
                    return RestrictionType.GeneralSop;
                case "maintenance":
                    return RestrictionType.Maintenance;
                default:
                    return RestrictionType.Other;
            }
        }
    }
}
